package com.joysuch.wwyt.datasync.service.impl;

import com.joysuch.wwyt.bp.entity.BpDeviceFacilityStopRecoverRecord;
import com.joysuch.wwyt.bp.entity.BpMainMaterial;
import com.joysuch.wwyt.datasync.support.AopDataSyncDaoInterceptor;
import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.entity.WorkFlowTask;
import com.joysuch.wwyt.workflow.repository.WorkFlowProcessDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.joysuch.wwyt.datasync.service.DataSyncRegister.loadFromJson;

/**
 * 将WorkFlow名称映射为 serviceName:${WorkFlowProcess.bizName}:${WorkFlowProcess.taskName}
 * 将BpMainMaterial映射为serviceName:${BpMainMaterial.type}
 * 将BpDeviceFacilityStopRecoverRecord映射为serviceName:${BpDeviceFacilityStopRecoverRecord.type}
 */
@Component
@Slf4j
@ConditionalOnProperty(value = "wwyt.feature.datasync.customer", havingValue = "nantong")
public class NantongDataSyncCondition extends  BaseDataSyncCondition{

    @Resource
    @Lazy
    private WorkFlowProcessDao workFlowProcessDao;

    @PostConstruct
    public void loadRegisterInfo() {
        try {
            loadFromJson(this.getClass().getClassLoader().getResourceAsStream("datasync-register-nantong.json"));
        } catch(Exception e) {
            throw new RuntimeException("加载南通配置文件错误", e);
        }
    }

    @Override
    public String getServiceName(AopDataSyncDaoInterceptor.Info info, Object entity, Object id) {
        if (info.getDomainClass() == WorkFlowTask.class) {
            return getWorkflowServiceName(info, (WorkFlowTask) entity);
        } else if (info.getDomainClass() == BpMainMaterial.class) {
            return info.getServiceName() + ":" + ((BpMainMaterial) entity).getType();
        } else if (info.getDomainClass() == BpDeviceFacilityStopRecoverRecord.class) {
            return info.getServiceName() + ":" + ((BpDeviceFacilityStopRecoverRecord) entity).getType();
        } else {
            // 返回原始值
            return info.getServiceName();
        }
    }

    private String getWorkflowServiceName(AopDataSyncDaoInterceptor.Info info, WorkFlowTask task) {
        WorkFlowProcess process = workFlowProcessDao.findById(task.getProcessId()).orElse(null);
        if (process != null) {
            // if ("隐患管理".equals(process.getBizName())) {
            // logger.debug("隐患管理: {}" + task);
            // if ("隐患排查".equals(task.getRefSource())) {
            // return info.serviceName + ":" + process.getBizName() + ":" + task.getPreTaskName();
            // } else {
            // // 隐患管理的其他值，不关心
            // return null;
            // }
            // } else {
            // // 非隐患管理的其他模块
            // }
            return info.getServiceName() + ":" + process.getBizName() + ":" + task.getPreTaskName();
        }
        log.warn("WorkFlowTask没有关联的WorkFlowProcess: {}" + task);
        return null;
    }
}
